package jlib.component.core
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.utils.Dictionary;
	
	import jlib.utils.MatrixUtil;

	public class DisplayManager {
		static private const g_map:Dictionary = new Dictionary(true);
		static private const g_list:Vector.<DisplayObject> = new Vector.<DisplayObject>();
		static private var g_shape:Shape;
		
		public function DisplayManager() {
			throw new Error("only static");
		}
		
		static public function registNoneScale($do:DisplayObject): void {
			if(!g_shape){
				g_shape = new Shape();
				g_shape.addEventListener(Event.ENTER_FRAME, onRender );
			}
			var mat:Matrix = calcNoneScale($do);
			g_map[$do] = $do.transform.concatenatedMatrix;
			$do.transform.matrix = mat;
			g_list.push($do);
		}
		
		static public function unregistNoneScale($do:DisplayObject): void {
			var idx:int = g_list.indexOf($do);
			g_list.splice(idx,1);
			g_map[$do] = null;
			delete g_map[$do];
		}
		
		static private function calcNoneScale($do:DisplayObject): Matrix {
			var inv:Matrix = $do.transform.concatenatedMatrix;
			inv.invert();
			var mat:Matrix = $do.transform.matrix;
			var tx:Number = mat.tx;
			var ty:Number = mat.ty;
			mat.concat(inv);
			mat.tx = tx;
			mat.ty = ty;
			return mat;
		}
		
		static private function onRender(event:Event): void {
			for each(var list:DisplayObject in g_list){
				if(!list.parent || !list.visible) continue ;
				if(MatrixUtil.equals(list.transform.concatenatedMatrix, g_map[list])) continue ;
				var mat:Matrix = calcNoneScale(list);
				list.transform.matrix = mat;
				g_map[list] = list.transform.concatenatedMatrix;
			}
		}		
		
	}//c
}